package com.lzp.common.batch;

import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;

/**
 * @author : lzp
 * @version 1.0
 * @date : 2022/10/18 18:59
 * @apiNote : excel导出账单的泛型Reader,根据不同的对接设置不同的列名和字段映射
 */
public class BillExcelReader<T> extends FlatFileItemReader<T> {
    private BillExcelReader() {
    }
    //todo 抽取出变化,做成策略模式,之后新增只需要提供变化的部分 csv映射列名,读取起始行(跳过读取的行数),读取结束标志位(是以空行结束还是以下划线之类的)

    public static <T> BillExcelReader<T> getInstance(FieldSetMapper<T> fieldSetMapper, String[] columnName) {
        DelimitedLineTokenizer delimitedLineTokenizer = new DelimitedLineTokenizer();
        delimitedLineTokenizer.setDelimiter(",");
        delimitedLineTokenizer.setStrict(false);
        delimitedLineTokenizer.setNames(columnName);
        DefaultLineMapper<T> defaultLineMapper = new DefaultLineMapper<>();
        defaultLineMapper.setLineTokenizer(delimitedLineTokenizer);
        defaultLineMapper.setFieldSetMapper(fieldSetMapper);
        BillExcelReader<T> billExcelReader = new BillExcelReader<>();
        billExcelReader.setLineMapper(defaultLineMapper);
        return billExcelReader;
    }
}
